package cn.dlc.com.user.service.impl;

import cn.dlc.com.user.dao.UserDao;
import cn.dlc.com.user.model.User;
import cn.dlc.com.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author 邓立川
 * @date 2021/11/21 18:39
 * @desc 当前类的描述信息
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userDao.loadUserByUsername(username);
        if(user != null) {

            String[] authenticates = new String[]{"p1"};
            // 获取权限(拿到一堆的权限标识符)
            if(user.getUsername().equals("rose")) {
                // 模拟rose多一个权限
                authenticates = new String[]{"p1","p2"};
            }

            // 这个authorities表示拥有的权限(可以是角色，先这样写)
            return org.springframework.security.core.userdetails.User.withUsername(user.getUsername())
                    // 取消明码："{noop}" +
                    .password(user.getPassword())
                    .authorities(authenticates)
                    // 不要一起用
                    // .roles(new String[]{"admin"})
                    .build();
        }

        return null;
    }
}
